package com.tikeyc.flutterandroid.custom;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tikeyc.flutterandroid.utils.VolumeChangeObserver;

import java.util.HashMap;

import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.BasicMessageChannel;
import io.flutter.plugin.common.StringCodec;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MyFlutterActivity extends FlutterActivity implements VolumeChangeObserver.VolumeChangeListener {
    private String Basic_Method_Channel = "com.tikeyc.fluttermodule/basicMethodChannel";

    private MyEventChannel myEventChannel;

    private VolumeChangeObserver volumeChangeObserver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initVolumn();
    }

    private void initVolumn() {
        //实例化对象并设置监听器
        volumeChangeObserver = new VolumeChangeObserver(this);
        volumeChangeObserver.setVolumeChangeListener(this);
        lastVolumn = volumeChangeObserver.getCurrentMusicVolume();
        Log.e("getCurrentMusicVolume", "initVolume = " + lastVolumn);
    }

    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        super.configureFlutterEngine(flutterEngine);
        GeneratedPluginRegistrant.registerWith(flutterEngine);

        //
        MyMethodChannel.instance(flutterEngine).initMethod();
        //
        MyBasicMessageChannel myBasicMessageChannel = MyBasicMessageChannel.instance(this, flutterEngine);
        myBasicMessageChannel.setMessageHandler();
        myBasicMessageChannel.send("Hello, this is BasicMethodChannel Send msg!", new BasicMessageChannel.Reply() {
            @Override
            public void reply(@Nullable Object reply) {
                Log.i("test", "收到了Flutter端发来的回复：" + reply);
            }
        });
        //
        myEventChannel = MyEventChannel.instance(flutterEngine).registerEventChannel();
        myEventChannel.getNetWorkType(getContext());
    }

    /////////////////// 必须复写以下方法，不然onCreate()、configureFlutterEngine()不会调用 ///////////////////
    @NonNull
    public static Intent createDefaultIntent(@NonNull Context launchContext) {
        return withNewEngine().build(launchContext);
    }

    @NonNull
    public static NewEngineIntentBuilder withNewEngine() {
        return new NewEngineIntentBuilder(MyFlutterActivity.class);
    }

    public static CachedEngineIntentBuilder withCachedEngine(@NonNull String cachedEngineId) {
        return new CachedEngineIntentBuilder(MyFlutterActivity.class, cachedEngineId);
    }
    /////////////////// 必须复写以上方法，不然onCreate()、configureFlutterEngine()不会调用 ///////////////////

    private int lastVolumn = 0;
    @Override
    public void onVolumeChanged(int volume) {
        //系统媒体音量改变时的回调
        Log.e("onVolumeChanged", "onVolumeChanged()--->volume = " + volume);
        if (myEventChannel != null && myEventChannel.eventSink != null) {
            HashMap params = new HashMap<>();
            params.put("type", "changeVolume");
            params.put("currentVolume", volume);
            params.put("des", volume > lastVolumn ? "音量键up" : "音量键down");
            myEventChannel.eventSink.success(params);
            lastVolumn = volume;
        }
    }

    @Override
    protected void onResume() {
        //注册广播接收器
        volumeChangeObserver.registerReceiver();
        super.onResume();
    }

    @Override
    protected void onPause() {
        //解注册广播接收器
        volumeChangeObserver.unregisterReceiver();
        super.onPause();
    }
}
